package server;

import server.mapper.Context;
import server.mapper.Host;
import server.mapper.Wrapper;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.List;
import java.util.Map;

public class RequestProcessor extends Thread {

    private Socket socket;
    private List<Host> mapper;

    public RequestProcessor(Socket socket, List<Host> mapper) {
        this.socket = socket;
        this.mapper = mapper;
    }

    @Override
    public void run() {
        try{
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());
            String url = request.getUrl();
            resolveUrl(url,request,response);

            // 静态资源处理
//            if(servletMap.get(request.getUrl()) == null) {
//                response.outputHtml(request.getUrl());
//            }else{
//                // 动态资源servlet请求
//                HttpServlet httpServlet = servletMap.get(request.getUrl());
//                httpServlet.service(request,response);
//            }
//
//            socket.close();

        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 判断是走动态资源还是静态资源呃
     *
     * @param url
     * @param request
     * @param response
     * @throws IOException
     */
    private void resolveUrl(String url, Request request, Response response) throws Exception {
        // 从webapps获取资源
        for (Host host : mapper) {
            if (!host.getName().equals("/")) {
                int first = url.indexOf("/");
                int second = url.indexOf("/", first + 1);
                List<Context> contextsList = host.getContexts();
                String secondStr = "/";
                if (second != -1) {
                    secondStr= url.substring(first + 1, second);
                }
                for (Context context : contextsList) {
                    if (context.getPath().equals(secondStr)) {
                        List<Wrapper> wrappers = context.getWrappers();
                        for (Wrapper wrapper : wrappers) {
                            if (wrapper.getUrlPattern().equals(url.substring(second))) {
                                wrapper.getServlet().service(request, response);
                                return;
                            }
                        }
                    }
                }
                // 当做静态资源处理
                for (Context context : contextsList) {
                    if (context.getPath().equals(secondStr)) {
                        response.outputHtml(host.getName() + "/" + request.getUrl());
                        return;
                    }
                }
            }
        }
        for (Host host : mapper) {
            if (host.getName().equals("/")) {
                for (Context context : host.getContexts()) {
                    for (Wrapper wrapper : context.getWrappers()) {
                        if (wrapper.getUrlPattern().equals(url)) {
                            wrapper.getServlet().service(request, response);
                            return;
                        }
                    }
                }
                // 当做静态资源处理
                response.outputHtml(url);
                return;
            }
        }
    }
}
